﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Runtime.Remoting.Messaging;
using System.Text;
using Baik.Interface.Execution;

namespace Baik
{
    public static class Program
    {
        public static BaikInstance Instance { get; set; }

        public enum ArgumentMode
        {
            File,
            Install,
            Update,
            Repo,
            Compile
        }

        public static void WriteCatch(string message, Exception ex)
        {
            Console.Out.WriteLine(message);
            Console.Out.WriteLine(ex.Message);
            Console.Out.WriteLine(ex.StackTrace);
            
        }
        public static void Main(string[] args)
        {
            Instance = new BaikInstance();
            var input = Console.In.ReadLine();
            new ArgumentHandler().GetArguments(args);
            while (input.Trim() != "quit")
            {
                try{Console.Out.WriteLine(Instance.Evaluate(input).AsString());}
                catch (Exception ex){WriteCatch(typeof(IExceptionHost).IsAssignableFrom(ex.GetType())? ((IExceptionHost) ex).Exception.AsString() : "UNHANDLED EXCEPTION" , ex);}
                input = Console.In.ReadLine();
            }

        }

        public class ArgumentHandler
        {
            public void GetArguments(string[] args)
            {
                var mode = ArgumentMode.File;
                foreach (var arg in args)
                {
                    switch (mode)
                    {
                        case ArgumentMode.File:
                            Instance.Environment.Load(arg);
                            break;
                        case ArgumentMode.Install:

                            /* global local rename
                            if (!isUrl(arg))
                            {
                                arg = GetUrlFromRepo(arg);
                            }
                            var meta = DownloadMetaFromUrl(arg);
                            var filename = meta.filename;
                            DownloadFile(filename, Instance.Environment.Eval("system.registry.globalLibraryDir").AsString());
                            if (filename.EndsWith(".dll")) AddExtensionLineTo;
                            */
                            break;
                        case ArgumentMode.Update:
                        /* all or name/url */
                        case ArgumentMode.Repo:
                            /* add remove  */
                            break;
                    }
                }
            }
        }
    }
}
